﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Bll.CommonBll;
using CSP.Bll.IObjectBlls;
using CSP.Model.CSP;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Dao;
using CSP.Common.Factories;
using CSP.Model.CSPEnum;

namespace CSP.Bll.ObjectBlls
{
    public class CreditLineBll : CSPCommonBll, ICreditLineBll
    {

        /// <summary>
        /// 缓存键值
        /// </summary>
        private const string CACHEKEY = "UserCredit_";

        private ICreditLineDao _creditLineDao;

        private CSPDaoFactory _factory;

        public CreditLineBll()
        {
            _cacheProvider = CachedProviderFactory.GetMemCachedProvider();
            _factory = CSPDaoFactory.GetInstance();
            _creditLineDao = _factory.GetCreditLineDao();
        }

        public Model.CSP.CreditLine GetDataById(int id)
        {
            throw new NotImplementedException();
        }

        public ICollection<Model.CSP.CreditLine> GeDatatByIds(int[] Ids)
        {
            throw new NotImplementedException();
        }

        [Obsolete("此方法已经废弃", true)]
        public void Add(Model.CSP.CreditLine obj)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 创建一条评论
        /// </summary>
        /// <param name="creditLine"></param>
        public void Create(CreditLine creditLine)
        {

            IUserCreditDao userCreditDao = _factory.GetUserCreditDao();
            //获取当前评价信息个数
            int count = _creditLineDao.FindCountByUsersAndOrderNumber(creditLine.FromUsername, creditLine.ToUserName, creditLine.OrderNumber);
            if (count != 0)
            {
                throw new Exception("已经评价过,不能重复评价!");
            }


            UserCredit userCredit = userCreditDao.GetByUser(creditLine.ToUserName);
            if (userCredit == null)
            {
                userCredit = GetCreatingUserCreditByCreditLine(creditLine);
                userCredit.CreditLines = new CreditLine[] { creditLine };
                userCreditDao.Add(userCredit);
            }
            else
            {
                userCredit = GetModifyUserCreditByCreditLine(userCredit, creditLine);
                userCreditDao.Update(userCredit);
                creditLine.UserCreditId = userCredit.id;
                _creditLineDao.Add(creditLine);
            }
            OrderRemindStatus orderRemindStatus = OrderRemindStatus.RatedBoth;

            IOrderRemindDao orderRemindDao = _factory.GetOrderRemindDao();
            OrderRemind orderRemind = orderRemindDao.GetDataByOrderNumber(creditLine.OrderNumber);
            //RatingType0消费者,1服务者
            if ((creditLine.RatingType == 0 && orderRemind.RemindStatus == (int)OrderRemindStatus.RatingConsume) ||
                (creditLine.RatingType == 1 && orderRemind.RemindStatus == (int)OrderRemindStatus.RatingService))
            {
                orderRemindStatus = OrderRemindStatus.RatedBoth;
            }
            else if (creditLine.RatingType == 0 && orderRemind.RemindStatus == (int)OrderRemindStatus.RatingBoth)
            {
                orderRemindStatus = OrderRemindStatus.RatingService;
            }
            else if (creditLine.RatingType == 1 && orderRemind.RemindStatus == (int)OrderRemindStatus.RatingBoth)
            {
                orderRemindStatus = OrderRemindStatus.RatingConsume;
            }
            orderRemind = GetOrderRemindByReminStatus(orderRemind, orderRemindStatus);
            orderRemindDao.Update(orderRemind);
        }

        public void Update(Model.CSP.CreditLine obj)
        {
            throw new NotImplementedException();
        }

        public void DeleteById(int id)
        {
            throw new NotImplementedException();
        }

        public void RefreshCache()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取需要创建的用户信誉
        /// </summary>
        /// <param name="creditLine"></param>
        /// <returns></returns>
        private UserCredit GetCreatingUserCreditByCreditLine(CreditLine creditLine)
        {
            UserCredit userCredit = new UserCredit();
            userCredit.ServiceAbility = creditLine.ServiceAbility;
            userCredit.ServiceAttitude = creditLine.ServiceAttitude;
            userCredit.username = creditLine.ToUserName;
            userCredit.CreditCount = 1;
            userCredit.CreateTime = DateTime.Now;

            //来自消费者
            if (creditLine.RatingType == 0)
            {
                userCredit.ConsumeCredit = GetGradeByRatingType(creditLine.CreditRating);
            }
            //来自服务者
            else
            {
                userCredit.ServiceCredit = GetGradeByRatingType(creditLine.CreditRating);
            }
            return userCredit;
        }

        /// <summary>
        /// 获取需要更新的信誉
        /// </summary>
        /// <param name="creditLine"></param>
        /// <returns></returns>
        private UserCredit GetModifyUserCreditByCreditLine(UserCredit userCredit, CreditLine creditLine)
        {

            userCredit.ServiceAbility += creditLine.ServiceAbility;
            userCredit.ServiceAttitude += creditLine.ServiceAttitude;
            //userCredit.username = creditLine.FromUsername;
            userCredit.CreditCount += 1;

            //userCredit.CreateTime = DateTime.Now;

            //来自消费者
            if (creditLine.RatingType == 0)
            {
                userCredit.ConsumeCredit += GetGradeByRatingType(creditLine.CreditRating);
            }
            //来自服务者
            else
            {
                userCredit.ServiceCredit += GetGradeByRatingType(creditLine.CreditRating);
            }
            return userCredit;
        }

        /// <summary>
        /// 根据信誉评价类型获取得分
        /// </summary>
        /// <param name="ratingType"></param>
        /// <returns></returns>
        private int GetGradeByRatingType(int ratingType)
        {
            int result = 0;
            if (ratingType == (int)CreditRatingType.Good)
            {
                result = 1;
            }
            else if (ratingType == (int)CreditRatingType.General)
            {
                result = 0;
            }
            else
            {
                result = -1;
            }
            return result;
        }

        /// <summary>
        /// 根据订单提醒状态 返回订单
        /// </summary>
        /// <param name="remindStatus"></param>
        /// <returns></returns>
        private OrderRemind GetOrderRemindByReminStatus(OrderRemind orderRemind, OrderRemindStatus remindStatus)
        {
            orderRemind.RemindStatus = (int)remindStatus;
            orderRemind.LostTime = DateTime.Now.AddDays(7);
            orderRemind.CreateTime = DateTime.Now;
            return orderRemind;
        }


        public List<int> FindIdsFromConsumeByServiceUser(string serviceUserName)
        {
            return _creditLineDao.FindIdsFromConsumeByServiceUser(serviceUserName);
        }

        public List<int> FindIdsFromServiceByConsumeUser(string consumeUserName)
        {
            return _creditLineDao.FindIdsFromServiceByConsumeUser(consumeUserName);
        }

        public List<int> FindIdsByFromUser(string fromUser)
        {
            return _creditLineDao.FindIdsByFromUser(fromUser);
        }

        public List<int> FindIdsFromConsumeByServiceId(int serviceId)
        {
            return _creditLineDao.FindIdsFromConsumeByServiceId(serviceId);
        }

        public ICollection<CreditLine> GetCreditLineByIds(List<int> ids, int pageIndex, int pageSize)
        {
            return _creditLineDao.GetCreditLineByIds(ids, pageIndex, pageSize);
        }


        public List<int> FindIdsByIdsAndCreditRatingType(List<int> ids, CreditRatingType creditRatingType)
        {
            return _creditLineDao.FindIdsByIdsAndCreditRatingType(ids, creditRatingType);
        }


        public List<int> FindIdsByIdsAndContentType(List<int> ids, int contentType)
        {
            return _creditLineDao.FindIdsByIdsAndContentType(ids, contentType);
        }
    }
}
